package com.starview.star.compiler.cafe.compile.cont;

import org.objectweb.asm.Opcodes;
import org.objectweb.asm.tree.FieldInsnNode;
import org.objectweb.asm.tree.InsnList;
import org.objectweb.asm.tree.MethodNode;

import com.starview.platform.data.type.Location;
import com.starview.star.code.repository.CodeCatalog;
import com.starview.star.compiler.ErrorReport;
import com.starview.star.compiler.cafe.compile.CafeDictionary;
import com.starview.star.compiler.cafe.compile.CodeContext;
import com.starview.star.compiler.cafe.compile.Expressions;
import com.starview.star.compiler.cafe.compile.HWM;
import com.starview.star.compiler.cafe.compile.ISpec;
import com.starview.star.compiler.cafe.compile.SrcSpec;



public class PutFieldCont implements IContinuation
{
  private final String owner;
  private final String fieldName;
  private final ISpec fieldSpec;
  private final CafeDictionary dict;

  public PutFieldCont(String owner, ISpec fieldSpec, String fieldName, CafeDictionary dict)
  {
    this.owner = owner;
    this.fieldName = fieldName;
    this.dict = dict;
    this.fieldSpec = fieldSpec;
  }

  @Override
  public ISpec cont(ISpec src, CafeDictionary cxt, Location loc, ErrorReport errors, CodeContext ccxt)
  {
    MethodNode mtd = ccxt.getMtd();
    HWM hwm = ccxt.getMtdHwm();
    CodeCatalog bldCat = ccxt.getBldCat();
    
    InsnList ins = mtd.instructions;
    Expressions.checkType(src, fieldSpec, mtd, dict, hwm, loc, errors, bldCat);
    ins.add(new FieldInsnNode(Opcodes.PUTFIELD, owner, fieldName, fieldSpec.getJavaSig()));
    return SrcSpec.prcSrc;
  }

  @Override
  public boolean isJump()
  {
    return false;
  }
}
